home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / shllutil.lha / shellutils-1.8 / src / dirname.c < prev    next >
C/C++ Source or Header  |  1991-08-29  |  1KB  |  58 lines

  1. /* dirname -- strip filename suffix from pathname
  2.    Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie and Jim Meyering. */
  19.  
  20. #include <stdio.h>
  21. #include <sys/types.h>
  22. #include "system.h"
  23.  
  24. void strip_trailing_slashes ();
  25.  
  26. void
  27. main (argc, argv)
  28.      int argc;
  29.      char **argv;
  30. {
  31.   register char *path;
  32.   register char *slash;
  33.  
  34.   if (argc != 2)
  35.     {
  36.       fprintf (stderr, "Usage: %s path\n", argv[0]);
  37.       exit (1);
  38.     }
  39.  
  40.   path = argv[1];
  41.   strip_trailing_slashes (path);
  42.  
  43.   slash = rindex (path, '/');
  44.   if (slash == NULL)
  45.     path = ".";
  46.   else
  47.     {
  48.       /* Remove any trailing slashes and final element. */
  49.       while (slash > path && *slash == '/')
  50.     --slash;
  51.       slash[1] = 0;
  52.     }
  53.   puts (path);
  54.  
  55.   exit (0);
  56. }
  57.  
  58.